package top.xia17.plugins.fast.common.model;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import top.xia17.plugins.fast.common.utils.ObjArray2EntitySign;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.List;

/**
 * 占比数据
 * @author xia17
 * @date 2021/1/14 14:41
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
public class ScaleData {

    /** 名字 */
    private String name ;

    /** 值 */
    private Integer num;

    /** 值2 */
    private Integer num2;

    /** 占比 */
    private double scale;

    /** 百分比 */
    private String scaleStr;

    /**
     * 计算占比
     * @param list /
     */
    public static void computeScale(List<ScaleData> list){
        NumberFormat nt = NumberFormat.getPercentInstance();
        //设置百分数精确度2即保留两位小数
        nt.setMinimumFractionDigits(2);
        int total = list.stream().mapToInt(ScaleData::getNum).sum();
        double totalScale = 0D;
        for (int i = 0; i < list.size(); i++) {
            ScaleData scaleData = list.get(i);
            double v = new BigDecimal(scaleData.num * 1.0D / total).setScale(4, RoundingMode.HALF_DOWN).doubleValue();
            if (i == list.size() -1){
                v = 1 - totalScale;
            }else {
                totalScale += v;
            }
            scaleData.setScale(v);
            scaleData.setScaleStr(nt.format(v));
        }
    }

    @ObjArray2EntitySign
    public ScaleData(String name, Integer num) {
        this.name = name;
        this.num = num;
    }

    @ObjArray2EntitySign(1)
    public ScaleData(Integer num, Integer num2) {
        this.num = num;
        this.num2 = num2;
    }

    @ObjArray2EntitySign(2)
    public ScaleData(String name, Integer num, Integer num2) {
        this.name = name;
        this.num = num;
        this.num2 = num2;
    }
}
